function setupJudgeGrid() {
    $("#judges").jqGrid({
        url : 'getJudgeList.action',
        datatype : "json",
        colNames : [ '序号','评委类别', '姓名', '性别', '籍贯', '出生日期', '最高学历','单位', '职位', '手机', '邮箱地址', '调用次数' ],
        colModel : [ { name : 'no', index : 'no', width : 25, sortable: false },
                     { name : 'categoryId', index : 'categoryId', width : 80},
                     { name : 'name', index : 'name', width : 60 },
                     { name : 'isMale', index : 'isMale', width : 30 },
                     { name : 'hometown', index : 'hometown', width : 80 },
                     { name : 'birthday', index : 'birthday', width : 80 },
                     { name : 'topEducation', index : 'topEducation', width : 60 },
                     { name : 'workUnit', index : 'workUnit', width : 100 },
                     { name : 'post', index : 'post', width : 60 },
                     { name : 'cellPhone', index : 'cellPhone', width : 100 },
                     { name : 'mailAddress', index : 'mailAddress', width : 100 },
                     { name : 'callTimes', index : 'callTimes', width : 60 }
                   ],
        rowNum : 10,
        height : 'auto',
        rowList : [ 10, 20, 30 ],
        pager : '#pager',
        sortname : 'id',
        viewrecords : true,
        sortorder : "desc",
        caption : "评委信息一览",
        multiselect : true,
        loadError : dealError
    });
    $("#judges").jqGrid('navGrid', '#pager', {
        edit : false,
        add : false,
        del : false
    });
}

function setupJudgeToolbar() {
    $("#add").button({
        icons : {
            primary : "ui-icon-plus"
        }
    }).click(function() {
        $("#id").val('');
        var params = { parentId : 3 };
        $.ajax({
            url : "listLevel.action",
            type : "post",
            dataType : "json",
            data : params,
            success : function(data) {
                initDropDownList(data);
                $("#detail").dialog("open");
            },
            error : dealError
        });
    });

    $("#edit").button({
        icons : {
            primary : "ui-icon-pencil"
        }
    }).click(function() {
        var selects = $('#judges').jqGrid('getGridParam', 'selarrrow');
        if (!selects || selects == '') {
            message('请选择需要更新的评委信息！', 'alert', 'top');
            return false;
        }
        if (selects.length > 1) {
            message('请只选择一条需要更新的评委信息！', 'alert', 'top');
            return false;
        }
        var params = {
            selectIds : selects.toString()
        };
        $.ajax({
            url : "updateInitJudge.action",
            type : "post",
            dataType : "json",
            data : params,
            success : showJudge,
            error : dealError
        });
    });

    $("#delete").button({
        icons : {
            primary : "ui-icon-close"
        }
    }).click(function() {
        var selects = $('#judges').jqGrid('getGridParam', 'selarrrow');
        if (!selects || selects == '') {
            message('请选择需要删除的评委信息！', 'alert', 'top');
            return false;
        }
        confirm('是否确定删除？', 'center', doDeleteJudge);
    });

    $("#judgeProperty").button({
        icons : {
            primary : "ui-icon-zoomout"
        }
    }).click(function() {
        var selects = $('#judges').jqGrid('getGridParam', 'selarrrow');
        if (!selects || selects == '') {
            message('请选择需要显示详细的评委信息！', 'alert', 'top');
            return false;
        }
        if (selects.length > 1) {
            message('请只选择一条需要显示详细的评委信息！', 'alert', 'top');
            return false;
        }
        $("#judgeId").val(selects[0]);
        $("#showDetailForm").submit();
    });

    $('#filter').button().click(function() {
        var sexCodeArray = getSexMatchCode($("#keyword").val());
        var educationCodeArray = getEducationMatchCode($("#keyword").val());
        var educationTypeCodeArray = getEducationTypeMatchCode($("#keyword").val());
        $('#unfilter').button('option', 'disabled', false);
        var judgeSearch = {
            'judgeSearch.idNo' : $("#keyword").val(),
            'judgeSearch.name' : $("#keyword").val(),
            'judgeSearch.sex' : -1,
            'judgeSearch.sexCodes' : sexCodeArray.join(),
            'judgeSearch.birthdayFrom' : '',
            'judgeSearch.birthdayTo' : '',
            'judgeSearch.graduatedFrom' : $("#keyword").val(),
            'judgeSearch.topEducation' : -1,
            'judgeSearch.topEducationCodes' : educationCodeArray.join(),
            'judgeSearch.educationType' : -1,
            'judgeSearch.educationTypeCodes' : educationTypeCodeArray.join(),
            'judgeSearch.subject' : $("#keyword").val(),
            'judgeSearch.workUnit' : $("#keyword").val(),
            'judgeSearch.title' : $("#keyword").val(),
            'judgeSearch.post' : $("#keyword").val(),
            'judgeSearch.skillsCert' : $("#keyword").val(),
            'judgeSearch.workPhone' : $("#keyword").val(),
            'judgeSearch.cellPhone' : $("#keyword").val(),
            'judgeSearch.imAddress' : $("#keyword").val(),
            'judgeSearch.mailAddress' : $("#keyword").val(),
            'judgeSearch.workAddress' : $("#keyword").val(),
            'judgeSearch.address' : $("#keyword").val(),
            'judgeSearch.trainExperience' : $("#keyword").val(),
            'judgeSearch.workExperience' : $("#keyword").val(),
            'judgeSearch.hometown' : $("#keyword").val(),
            'judgeSearch.homePhone' : $("#keyword").val(),
            'judgeSearch.speciality' : $("#keyword").val(),
            'judgeSearch.remark' : $("#keyword").val(),
            'judgeSearch.isOr' : true
        };
        var postData = $("#judges").jqGrid("getGridParam", "postData");
        $.extend(postData, judgeSearch);
        $("#judges").jqGrid("setGridParam", {
            search: true
        }).trigger("reloadGrid", [{page:1}]);
    });
    $('#unfilter').button({ disabled : true }).click(function() {
        $(this).button('option', 'disabled', true);
        var judgeSearch = {
                'judgeSearch.idNo' : '',
                'judgeSearch.name' : '',
                'judgeSearch.sex' : -1,
                'judgeSearch.sexCodes' : '',
                'judgeSearch.birthdayFrom' : '',
                'judgeSearch.birthdayTo' : '',
                'judgeSearch.graduatedFrom' : '',
                'judgeSearch.topEducation' : -1,
                'judgeSearch.topEducationCodes' : '',
                'judgeSearch.educationType' : -1,
                'judgeSearch.educationTypeCodes' : '',
                'judgeSearch.subject' : '',
                'judgeSearch.workUnit' : '',
                'judgeSearch.title' : '',
                'judgeSearch.post' : '',
                'judgeSearch.skillsCert' : '',
                'judgeSearch.workPhone' : '',
                'judgeSearch.cellPhone' : '',
                'judgeSearch.imAddress' : '',
                'judgeSearch.mailAddress' : '',
                'judgeSearch.workAddress' : '',
                'judgeSearch.address' : '',
                'judgeSearch.trainExperience' : '',
                'judgeSearch.workExperience' : '',
                'judgeSearch.hometown' : '',
                'judgeSearch.homePhone' : '',
                'judgeSearch.speciality' : '',
                'judgeSearch.remark' : '',
                'judgeSearch.isOr' : true
            };
            var postData = $("#judges").jqGrid("getGridParam", "postData");
            $.extend(postData, judgeSearch);
            $("#judges").jqGrid("setGridParam", {
                search: true
            }).trigger("reloadGrid", [{page:1}]);
    });
}

function doDeleteJudge() {
    var selects = $('#judges').jqGrid('getGridParam', 'selarrrow');
    var params = {
        selectIds : selects.toString()
    };
    $.ajax({
        url : "deleteJudge.action",
        type : "post",
        dataType : "json",
        data : params,
        beforeSend : validate,
        success : reload,
        error : dealError
    });
}

function validate(xhr) {
    ;
}

function validateJudge() {
    if ($('#idNo').val() == '') {
        message('请输入身份证号！', 'alert', 'top');
        return false;
    }
    if ($('#name').val() == '') {
        message('请输入姓名！', 'alert', 'top');
        return false;
    }
    return true;
}

function setupJudgeDialog() {
    $("#detail").dialog({
        autoOpen : false,
        show : "slide",
        height : 600,
        width : 600,
        modal : true,
        buttons : {
            登录 : function() {
                var level2Value = $('#level2').val();
                var level1Value = $('#level1').val();
                if (level2Value && level2Value != '') {
                    $('#categoryId').val(level2Value);
                } else if (level1Value && level1Value != '') {
                    $('#categoryId').val(level1Value);
                }

                if (!$('#categoryId').val() || $('#categoryId').val() == '') {
                    message('评委分类设定失败', 'alert', 'top');
                    return false;
                }

                var params = $("#judgeDetail").serialize();
                $.ajax({
                    url : "saveJudge.action",
                    type : "post",
                    dataType : "json",
                    data : params,
                    beforeSend : validateJudge,
                    success : doUpload,
                    error : dealError
                });
                return false;
            },
            取消 : function() {
                $(this).dialog("close");
                return false;
            }
        },
        close : function() {
            $("input").val("").removeClass("ui-state-error");
            $("select").val("").removeClass("ui-state-error");
            $("textarea").val("").removeClass("ui-state-error");
        }
    });

    $('#level1').change(function() {
        level1Change();
    });
}

function level1Change(level2Value) {
    if ($('#level1').val() == '') {
        $("#level2 option").remove();
        return false;
    }
    var params = {
        parentId : $('#level1').val()
    };
    $.ajax({
        url : "listLevel.action",
        type : "post",
        dataType : "json",
        data : params,
        success : function(data) {
            $("#level2 option").remove();
            var categorys = data.categorys;
            var size=categorys.length;
            for (var i=0; i<size; i++) {
                var id = categorys[i].id;
                var name = categorys[i].name;
                var option = "<option value=" + id + ">" + name + "</option>";
                $("#level2").append(option);
            }
            if (level2Value) {
                $('#level2').val(level2Value);
            }
        },
        error : dealError
    });
}

function doUpload() {
    if ($('#photo').val() != '') {
        var idNo = $('#idNo').val();
        ajaxPhotoUpload(idNo);
    }

    $('#detail').dialog('close');
    reload();
}

function reload() {
    $("#judges").trigger("reloadGrid");
}

function initDropDownList(data) {
    $("#level1 option").remove();
    var categorys = data.categorys;
    for ( var i = 0; i < categorys.length; i++) {
        var id = categorys[i].id;
        var name = categorys[i].name;
        var option = "<option value=" + id + ">" + name + "</option>";
        $("#level1").append(option);
    }
}

function showJudge(result, status) {
    initDropDownList(result);
    var data = result.judge;
    $("#detail").dialog("open");
    $("#id").val(data.id);
    $("#name").val(data.name);
    $("#idNo").val(data.idNo);
    $("#sex").val(data.sex);
    $("#birthday").val(data.birthday);
    $("#graduatedFrom").val(data.graduatedFrom);
    $("#topEducation").val(data.topEducation);
    $("#educationType").val(data.educationType);
    $("#subject").val(data.subject);
    $("#workUnit").val(data.workUnit);
    $("#title").val(data.title);
    $("#post").val(data.post);
    $("#skillsCert").val(data.skillsCert);
    $("#workPhone").val(data.workPhone);
    $("#cellPhone").val(data.cellPhone);
    $("#imAddress").val(data.imAddress);
    $("#mailAddress").val(data.mailAddress);
    $("#workAddress").val(data.workAddress);
    $("#address").val(data.address);
    $("#trainExperience").val(data.trainExperience);
    $("#workExperience").val(data.workExperience);
    $("#hometown").val(data.hometown);
    $("#homePhone").val(data.homePhone);
    $("#speciality").val(data.speciality);
    $('#regDate').val(data.regDate);
    $('#remark').val(data.remark);
    $('#isSigned').val(data.isSigned);
    $('#categoryId').val(data.categoryId);
    $('#level1').val(result.level1);
    level1Change(result.level2);
}

function ajaxPhotoUpload(idNo) {
    $("#loading").ajaxStart(function() {
        $(this).show();
    }).ajaxComplete(function() {
        $(this).hide();
    });
    $.ajaxFileUpload({
        url : 'judgePhotoUpload.action?idNo=' + idNo,
        secureuri : false,
        fileElementId : 'photo',
        dataType : 'json',
        success : function(data, status) {
            if (data.errorMsg) {
                message('上传文件处理失败：' + data.errorMsg, 'alert', 'top');
            }
        }
    });
}

function setupJudgeImport() {
    var params = { parentId : 3 };
    $.ajax({
        url : "listLevel.action",
        type : "post",
        dataType : "json",
        data : params,
        success : function(data) {
            $("#level1 option").remove();
            var categorys = data.categorys;
            for ( var i = 0; i < categorys.length; i++) {
                var id = categorys[i].id;
                var name = categorys[i].name;
                var option = "<option value=" + id + ">" + name + "</option>";
                $("#level1").append(option);
            }
        },
        error : dealError
    });

    $('#level1').change(function() {
        level1Change();
    });

    $("#upload").button({
        icons : {
            primary : "ui-icon-arrowthick-1-n"
        }
    }).click(function() {
        if ($('#judgeExcel').val() == '') {
            return false;
        }
        ajaxJudgeImport();
    });
    
    $('#templateXls').button({
        icons : {
            primary: "ui-icon-copy"
        }
    });
    $('#templateDoc').button({
        icons : {
            primary: "ui-icon-copy"
        }
    });
}

function ajaxJudgeImport() {
    $("#loading").ajaxStart(function() {
        $(this).show();
    }).ajaxComplete(function() {
        $(this).hide();
    });

    var level2Value = $('#level2').val();
    var level1Value = $('#level1').val();
    var categoryId = '';
    if (level2Value && level2Value != '') {
        categoryId = level2Value;
    } else if (level1Value && level1Value != '') {
        categoryId = level1Value;
    }

    $.ajaxFileUpload({
        url : 'judgeExcelImport.action',
        secureuri : false,
        fileElementId : 'judgeExcel',
        dataType : 'json',
        data : {
            'excelJudge.categoryId' : categoryId,
            'excelJudge.remark' : $('#remark').val()
        },
        success : function(data, status) {
            if (data.errorMsg) {
                message('上传文件处理失败：' + data.errorMsg, 'alert', 'top');
            } else {
                message('上传文件处理成功', 'alert', 'top');
            }
        }
    });
}
